<?php

namespace App\Models;


class Coupon extends BaseModel
{
    protected $table = 'coupons';

    protected $fillable = [
        'name',
        'type', //1 满减，2 折扣
        'amount',
        'discount',
        'max_dec',
        'min_amount',
        'num',
        'status',
    ];

    public static function getAmountLes($coupon_user_id, $amount_all){
        $couponUser = CouponUser::where('id', $coupon_user_id)->first();
        $coupon = Coupon::where('id', $couponUser->coupon_id)->first();
        if($coupon->min_amount > 0 && $coupon->min_amount < $amount_all){
            return 0;
        }
        $les = 0;
        if ($coupon->type == 1){
            $les = $coupon->amount;
        } else if($coupon->type == 2){
            $les = $amount_all * $coupon->discount;
        }
        if($coupon->max_dec > 0){
            $les = min($coupon->max_dec, $les);
        }
        return $les;
    }
}
